package com.wink_172.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.util.Log
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.wink_172.adapter.HBomeListAdapter
import com.wink_172.commons.R
import com.wink_172.commons.databinding.ActivityUploadBinding
import com.wink_172.library.activity.SmartActivity
import com.wink_172.library.adapter.HomeListAdapter
import com.wink_172.library.callback.ICallback
import com.wink_172.library.model.BaseDataItem
import com.wink_172.library.model.json.FileData
import com.wink_172.library.utils.CommonUtils
import com.wink_172.library.utils.TencentCosUtils
import com.wink_172.library.view.ActionBarView
import com.wink_172.library.view.CustomRecyclerView
import com.wink_172.model.Constants
import java.io.File
import java.util.ArrayList

/**
 * 文件 文件夹上传
 *
 *
 * 可用于日志同步  文件同步分析
 *
 * 前提必须获得存储权限
 * 未做
 * */
class UploadActivity : SmartActivity() {

    lateinit var binding: ActivityUploadBinding


    companion object {
        @JvmStatic
        fun startActivity(context: Context, vararg args: Any?) {
            val intent = Intent(context, UploadActivity::class.java)

            //intent.putExtra(Constants.PARAM_DATA1, (String) args[0]);
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this, R.layout.activity_upload)
        binding?.setOnClickListener(this)
        setStatusBarLightMode()
        initView()
        init()
    }

    private fun initView() {
        (binding!!.actionBar as ActionBarView).updateAllContent(
            com.wink_172.library.R.mipmap.icon_back,
            "文件上传  (日志同步)",
            ""
        );
        (binding!!.actionBar as ActionBarView).setCallback(object : ActionBarView.ICallback {
            override fun onSendEvent(event: Int, content: String?) {
                if (event == ActionBarView.LEFT_BTN) {
                    finish()
                }
            }
        })


        val list: List<Any?> = ArrayList()
        val homeListAdapter = HBomeListAdapter(this, list, binding!!.listView)
        binding!!.listView.init(homeListAdapter, false)
        val layoutManager = LinearLayoutManager(binding!!.root.context)


        binding!!.listView.layoutManager = layoutManager


        binding!!.listView.setCallback(object : CustomRecyclerView.ICallback {
            override fun onLoadMore() {

            }
        })
    }

    fun init() {
        val list_data: MutableList<FileData>? = ArrayList();
        val fileData = FileData()
        fileData.local_url = com.wink_172.library.model.Constants.savePath + "";
        fileData.web_url = com.wink_172.library.model.Constants.savePath + "";
        fileData.name = "天天跳舞 目录同步"
        list_data!!.add(fileData)

        initList(list_data)

        for (i in 0..10 - 1) {


        }
    }

    private val TAG = "UploadActivity"

    /**
     * 上传文件夹 local_url是本地文件夹地址
     * 批量上传文件
     * */
    fun uploadFileList(fileData: FileData) {
        val dirSource: File = File(fileData.local_url)
        if (!dirSource.isDirectory) {
            return
        }

        showProgressDialog(true)
        val childList = dirSource.list()
        if (childList != null && childList.size > 0) {
            for (fileName in childList) {
                val local_target_url = fileData.local_url + fileName
                val cosPath: String =
                    com.wink_172.library.model.Constants.TENCENT_OSS_PACKAGE_NAME_temp_fd + fileName
//                            FileUtil.getExtensionName(fileName) //对象在存储桶中的位置标识符，即称对象键

//                TencentCosUtils.uploadTectentFile(cosPath, local_target_url,
//                    object :
//                       com.wink_172.library.callback.ICallback {
//                        override fun onSendEvent(event: Int, vararg args: Any?) {//这个callback用的非常巧妙
//                            when (event) {
//                                com.wink_172.library.model.Constants.EVENT_01 -> {
//                                    handler!!.removeMessages(100)
//                                handler!!.sendEmptyMessageDelayed(100,1000);
//                                }
//                                else -> {
//
//                                    handler!!.removeMessages(101)
//                                    handler!!.sendEmptyMessageDelayed(101,1000);
//                                }
//                            }
//                        }
//                    })
            }
        }
    }

    override fun handlerCallBack(msg: Message?) {
        super.handlerCallBack(msg)

        when(msg!!.what){
            100->{
                CommonUtils.showToast("上传同步成功")
                hideProgressDialog()
            }
            101->{
                CommonUtils.showToast("同步失败")
                hideProgressDialog()
            }

        }

    }

    /**
     *
     * * */
    fun initList(list_data: MutableList<FileData>) {
        val list: MutableList<Any> = ArrayList()
        for (i in 0..list_data.size - 1) {
            val baseDataItem = BaseDataItem(i, Constants.SCENE_DOWN_LOAD_LIST)
            val info12 = list_data.get(i) as FileData
            baseDataItem.setObjectData(info12)
            baseDataItem.callback = object : HomeListAdapter.BaseViewsHolder.ICallback {
                override fun postObject(event: Int, vararg args: Any?) {
                    when (event) {
                        com.wink_172.library.model.Constants.EVENT_01 -> {

                            uploadFileList(info12)
                        }
                    }
                }
            }
            list.add(baseDataItem)
        }

        val homeListAdapter = binding!!.listView.adapter as HBomeListAdapter?
        if (binding!!.swipeRefreshView.isRefreshing) {

            binding!!.listView.currentPage = 1
            homeListAdapter!!.swapContent(list)
            homeListAdapter.notifyDataSetChanged()
        } else if (list.size > 0) {
            binding!!.listView.currentPage = (binding!!.listView.currentPage + 1)
            homeListAdapter!!.appendContent(list, false)
            homeListAdapter.notifyDataSetChanged()
        }
        binding!!.swipeRefreshView.isRefreshing = false
        binding!!.listView.isLoadingMore = false
    }

}